<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>js进阶自习作业</title>
</head>

<body>
    <script>
        // 要求：必须使用上 es5/es6 相关的语法
        // 比如: 展开运算符，数组reduce，filter，map等语法...

        // 1. 封装函数，求得数组中的最大值  [1, 6, 2, 8, 11, 5]
        function getMax(arr) {
            return Math.max(...arr)
        }
        let arr = [1, 6, 2, 8, 11, 5]
        let max = getMax(arr)
        console.log(max)

        // --------------------------------------------------------------

        // 2. 封装函数，求得传入数组元素的和 [3, 9, 9, 2, 7, 5]
        function getSum(arr) {
            return arr.reduce((total, item) => total + item, 0)
        }
        let arr2 = [3, 9, 9, 2, 7, 5]
        let sum2 = getSum(arr2)
        console.log(sum2)

        // --------------------------------------------------------------

        // 3. 封装函数，求得传入数组⾥⼤于 5 的和 [4, 9, 6, 90, 3, 11]
        function getLargeSum(arr) {
            return arr.filter(item => item > 5).reduce((total, item) => total + item, 0)
        }
        let arr3 = [4, 9, 6, 90, 3, 11]
        let sum3 = getLargeSum(arr3)
        console.log(sum3)

        // --------------------------------------------------------------
        // 4. 对数组，按照价格进行升序排序
        const arr4 = [
            { id: 1, name: '茶杯', price: 18 },
            { id: 2, name: '电视机', price: 2000 },
            { id: 3, name: '牙刷', price: 8 },
            { id: 4, name: '电脑', price: 5000 },
        ]

        function priceUp(arr) {
            for (let i = 1; i < arr.length; i++) {
                for (let j = 0; j < arr.length - 1; j++) {
                    if (arr[j].price > arr[j + 1].price) {
                        let tamp = arr[j]
                        arr[j] = arr[j + 1]
                        arr[j + 1] = tamp
                    }
                }
            }
        }
        priceUp(arr4)
        console.log(arr4)
        // ---------------------------------------------------------------
        // 5. 将学生数组中的所有姓名，存入成一个新数组，并过滤掉小强 => 得到 ['小明', '小丽', '小美']
        const arr5 = [
            { id: 1, name: '小强', score: 18 },
            { id: 2, name: '小明', score: 2000 },
            { id: 3, name: '小丽', score: 8 },
            { id: 4, name: '小美', score: 5000 },
        ]

        function getName(arr) {
            const newArr = arr.filter(item => item.name !== '小强')
            const newArr2 = []
            newArr.forEach(item => newArr2.push(item.name))
            return newArr2
        }

        console.log(getName(arr5))
    </script>
</body>

</html>